package com.vitaf.cash.app.base.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.viewbinding.ViewBinding

/**
 * DialogFragment 基类
 * @param T ViewBinding 类型
 */
abstract class BaseDialogFragment<T : ViewBinding> : DialogFragment() {
    protected var _binding: T? = null
    protected val binding get() = _binding!!

    // 是否可点击外部取消
    protected open val cancelable: Boolean = true

    // 对话框宽度
    protected open val dialogWidth: Int = ViewGroup.LayoutParams.MATCH_PARENT

    // 对话框高度
    protected open val dialogHeight: Int = ViewGroup.LayoutParams.WRAP_CONTENT

    // 对话框重力
    protected open val gravity: Int = android.view.Gravity.CENTER

    // 对话框动画
    protected open val windowAnimations: Int? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, getDialogStyle())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = createBinding(inflater, container)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initDialog()
        initView()
        initData()
        setListeners()
    }

    override fun onStart() {
        super.onStart()
        setupDialogWindow()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    /**
     * 创建 ViewBinding
     */
    protected abstract fun createBinding(inflater: LayoutInflater, container: ViewGroup?): T

    /**
     * 获取对话框样式
     */
    protected open fun getDialogStyle(): Int {
        return androidx.appcompat.R.style.Theme_AppCompat_Dialog
    }

    /**
     * 初始化对话框属性
     */
    protected open fun initDialog() {
        isCancelable = cancelable
    }

    /**
     * 初始化视图
     */
    protected abstract fun initView()

    /**
     * 初始化数据
     */
    protected open fun initData() {
        // 子类可重写
    }

    /**
     * 设置监听器
     */
    protected open fun setListeners() {
        // 子类可重写
    }

    /**
     * 设置对话框窗口属性
     */
    protected open fun setupDialogWindow() {
        dialog?.window?.apply {
            setLayout(dialogWidth, dialogHeight)
            setGravity(gravity)

            windowAnimations?.let {
                setWindowAnimations(it)
            }

            // 设置背景透明，避免直角
            setBackgroundDrawableResource(android.R.color.transparent)
        }
    }

    /**
     * 安全显示对话框
     */
    fun showSafe(manager: androidx.fragment.app.FragmentManager, tag: String? = null) {
        if (!isAdded && !isVisible) {
            show(manager, tag ?: this::class.java.simpleName)
        }
    }

    /**
     * 安全关闭对话框
     */
    fun dismissSafe() {
        if (isAdded && isVisible) {
            dismiss()
        }
    }
}